package com.sxd.imp.flow.label.simple;

import java.math.BigInteger;


public class JIangTest {

    public static BigInteger ONE = new BigInteger("1");
    public static BigInteger TWO = new BigInteger("2");

    public static void main(String[] args) {
        int a = 123456;
        int b = 543216;
        int n ;
        int data = 123456;
        // 创建密钥 n  (a,n)  (b,n)
        n = getKey(a,b);
        {
            long time = System.currentTimeMillis();
            // 加密
            int aa = decode(data, a, n);
            System.out.println((System.currentTimeMillis() - time) + "ms");
            System.out.println(aa);
            time = System.currentTimeMillis();
            // 解密
            System.out.println(decode(aa, b, n));
            System.out.println((System.currentTimeMillis() - time) + "ms");
        }
    }

    public static int decode(int data, int key, int n) {
        BigInteger a = new BigInteger(Integer.toString(data));
        a = a.multiply(new BigInteger(Integer.toString(key)));
        BigInteger b = new BigInteger(Integer.toString(n));
        return a.mod(b).intValue();
    }

    public static int getKey(int ia, int ib) {
        long time = System.currentTimeMillis();
        BigInteger a = new BigInteger(Integer.toString(ia));
        BigInteger b = new BigInteger(Integer.toString(ib));
        BigInteger c = a.multiply(b);
        BigInteger i = new BigInteger("2");
        c = c.subtract(ONE);
        System.out.println(c);
        int[] data = new int[10];
        int index = 0;
        while (c.compareTo(i) > 0) {
            if (c.mod(i).intValue() == 0) {
                data[index++] = i.intValue();
//                System.out.print(i + "  ");
                c = c.divide(i);
                i = new BigInteger("2");
            } else {
                i = i.add(ONE);
            }
        }
        System.out.print(c);
        System.out.println();
        data[index++] = c.intValue();
        int n = data[--index];
        while (n < ia) {
            n *= data[--index];
        }
        System.out.println(n);
        System.out.println((System.currentTimeMillis() - time) + "ms");
        return n;
    }

    public static void baoli(int a, int n) {
        BigInteger ba = new BigInteger(Integer.toString(a));
        BigInteger bn = new BigInteger(Integer.toString(n));
        BigInteger i = bn.add(bn);
        int i2 = 2;
        while (i.add(BigInteger.ONE).mod(ba).intValue() != 0) {
            i = i.add(bn);
        }
        i = i.add(BigInteger.ONE).divide(ba);
        System.out.println(a + "*" + i.intValue() + "%" + n + "=1");
    }
}
